package com.example.demo.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/msg")
@RequiredArgsConstructor
@Slf4j
public class MsgController {

    private final RabbitTemplate rabbitTemplate;

    @GetMapping("/send/{queueName}/{msg}")
    public String sendMsg(@PathVariable String queueName,@PathVariable String msg) {
        rabbitTemplate.convertAndSend(queueName, msg);
        log.info("已往{}发送消息: {}", queueName,msg);
        return "已往" + queueName + "发送消息: " + msg;
    }

    @GetMapping("/sendToExchange/{exchangeName}/{key}/{msg}")
    public String sendMsgToExchange(@PathVariable String exchangeName,@PathVariable String key,@PathVariable String msg) {
        rabbitTemplate.convertAndSend(exchangeName, key, msg);
        log.info("已往{},key：{}，发送消息: {}", exchangeName,key,msg);
        return "已往" + exchangeName + ",key：" + key + "，发送消息: " + msg;
    }
}
